//
//  String+Global.swift
//  DF_Student
//
//  Created by 刘盖 on 2023/12/9.
//  Copyright © 2023 DFJGED. All rights reserved.
//

import Foundation

public extension NSString {
    
    //app版本
    @objc
    static var appVersion: String {
        return (Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String) ?? ""
    }
    
    //app版本转字符串(去除.)
    @objc
    static var appVersionForInt: Int {
        return Int(self.appVersion.replacingOccurrences(of: ".", with: "")) ?? 0
    }
    
    //app build号
    @objc
    static var appBuildVersion: String {
        return (Bundle.main.infoDictionary?["CFBundleVersion"] as? String) ?? ""
    }
    
    @objc
    static var osVersion: String {
        return UIDevice.current.systemVersion
    }
    
    @objc
    static var deviceName: String {
        return "\(UIDevice.current.name)_\(UIDevice.current.model)_\(UIDevice.current.systemVersion)"
    }
    
  
    
    
    
    

}


public extension String {
    static func formatOutputNumberString(_ string: Any?, maxDecimalCount: UInt) -> String{
        guard let string = string, !(string is NSNull) else {
            return "0"
        }
        
        var stringTemp = "0"
        if let temp = string as? String {
            stringTemp = temp
        }
        
        if let temp = string as? Int {
            stringTemp = String(format: "%f", Double(temp))
        }
        
        if let temp = string as? Float {
            stringTemp = String(format: "%f", Double(temp))
        }
        
        if let temp = string as? Double {
            stringTemp = String(format: "%f", Double(temp))
        }
    
        let arr = stringTemp.components(separatedBy: ".")
        if arr.count == 0 {
            return "0";
        }
        if (arr.count == 1) {
            return arr.first as! String
        }
        
        if Int(arr[1] as! String) == 0 {
            return arr.first as! String
        }
        
        var strTemp = arr[1]
        
        if strTemp.count > maxDecimalCount {
            let startIndex = strTemp.startIndex
            let endIndex = strTemp.index(startIndex, offsetBy: Int(maxDecimalCount))
            strTemp = String(strTemp[startIndex..<endIndex])
        }
        
        var finalString = "\(strTemp)"
        for item in strTemp {
            if finalString.hasSuffix("0") {
                finalString.removeLast()
            }
        }
        return "\(arr[0]).\(finalString)"
    }
}
